// dlgmodeless.cpp : implementation file
//

#include "stdafx.h"
#include "dlgmodeless.h"
#include "ObjectGenerator.h"
#include "AutoCADGMS.h"
#include "ProjectManager.h"
#include "NewPartCommand.h"
#include "UeEntityOperation.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgModeless dialog

typedef ObjectGenerator<CDlgModeless> ModelessGenerator;
ModelessGenerator DlgGenerator;
CDlgModeless& GetModelessDialog()
{
	CDlgModeless& modelessdlg = *DlgGenerator.Generate();
	if(modelessdlg.GetSafeHwnd() == NULL)
	{
		modelessdlg.Create(CDlgModeless::IDD);
		modelessdlg.ShowWindow(SW_HIDE);
	}
	return modelessdlg;
}

CDlgModeless::CDlgModeless(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgModeless::IDD, pParent)
{
	INIT_RESOURCE
	//{{AFX_DATA_INIT(CDlgModeless)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgModeless::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgModeless)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgModeless, CDialog)
	//{{AFX_MSG_MAP(CDlgModeless)
	ON_WM_CREATE()
	ON_MESSAGE(WM_NEWAPPDOCUMENT, OnNewAppDocment)
	ON_MESSAGE(WM_OPENAPPDOCUMENT, OnOpenAppDocment)
	ON_MESSAGE(WM_CLOSEAPPDOCUMENT, OnCloseAppCocment)
	ON_MESSAGE(WM_NEWPART, OnNewPart)
	ON_MESSAGE(WM_SAVEAPPDOCUMENT, OnSaveAppDocument)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgModeless message handlers

LRESULT CDlgModeless::OnNewPart(WPARAM wParam, LPARAM)
{
	Acad::ErrorStatus es;
	NewPartCommand command;
	es = acDocManager->lockDocument(acDocManager->curDocument());
	if(command.Execute())
	{
		SendCommandToAutoCAD(_T("Regen\n"));
//		es = acedGetAcadTextCmdLine()->PostMessage(, _T("Regen\n"));
		this->PostMessage(WM_NEWPART);
	}
	es = acDocManager->unlockDocument(acDocManager->curDocument());
	return 0;
}

LRESULT CDlgModeless::OnNewAppDocment(WPARAM wParam, LPARAM lParam)
{
    static TCHAR pData[] = "acad.dwt";
	AutoCADGMS *pGMS = (AutoCADGMS*)wParam;

    Acad::ErrorStatus st = acDocManager->appContextNewDocument((const TCHAR *)pData);
	ASSERT(st == Acad::eOk);

	if(pGMS == NULL)
		return 0;

	AcApDocument* pDoc = acDocManager->curDocument();
	ASSERT(pDoc);
	AcDbDatabase* pDB = pDoc->database();
	ASSERT(pDB);

	st = acDocManager->lockDocument(pDoc);
	pGMS->Database(pDB);
	pGMS->Document(pDoc);
	pGMS->Save();
	st = acDocManager->unlockDocument(pDoc);

	return 0;
}

LRESULT CDlgModeless::OnOpenAppDocment(WPARAM wParam, LPARAM lParam)
{
	TCHAR* pData = (TCHAR*)lParam;
	AutoCADGMS *pGMS = (AutoCADGMS*)wParam;
	
	Acad::ErrorStatus st;
	st = acDocManager->appContextOpenDocument((const TCHAR *)pData);
	ASSERT(st == Acad::eOk);
	delete pData;

	if(pGMS == NULL)
		return 0;

	AcApDocument* pDoc = acDocManager->curDocument();
	ASSERT(pDoc);
	AcDbDatabase* pDB = pDoc->database();
	ASSERT(pDB);
	
	st = acDocManager->lockDocument(pDoc);
	pGMS->Database(pDB);
	pGMS->Document(pDoc);
	st = acDocManager->unlockDocument(pDoc);
	return 0;
}

int CDlgModeless::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	END_RESOURCE
	
	return 0;
}

extern BOOL IsPrjDocument(AcApDocument* pDoc, Project* pPrj);
LRESULT CDlgModeless::OnCloseAppCocment(WPARAM wParam, LPARAM lParam)
{
	AcApDocument* pDoc = (AcApDocument*)lParam;
	ASSERT(pDoc);

	if(IsPrjDocument(pDoc, GetProjectManager().CurProject()))
		return 0;

	Acad::ErrorStatus es = acDocManager->closeDocument(pDoc);
	ASSERT(es == Acad::eOk);
	
/*	INT nCount = acDocManager->documentCount();
	if(nCount <= 1)
	{
		this->PostMessage(WM_NEWAPPDOCUMENT, 0, 0);
	}*/
	return 0;
}

LRESULT CDlgModeless::OnSaveAppDocument(WPARAM wParam, LPARAM lParam)
{
	AcDbDatabase *pDB = (AcDbDatabase*)lParam;
	if(pDB == NULL)
		return 0;

	ASSERT(pDB);

	TCHAR* pData = (TCHAR*)wParam;
	ASSERT(pData);

	Acad::ErrorStatus st = Acad::eOk;
	AcApDocument* pDoc = acDocManager->curDocument();
	st = acDocManager->lockDocument(pDoc);
	st = pDB->saveAs(pData);
	ASSERT(st == Acad::eOk);
	st = acDocManager->unlockDocument(pDoc);

	delete pData;
	
	return 0;
}

LRESULT CDlgModeless::OnActiveAppCocument(WPARAM wParam, LPARAM lParam)
{
	AcApDocument* pDoc = (AcApDocument*)lParam;
	ASSERT(pDoc);

	acDocManager->activateDocument(pDoc);

	return 0;
}